package day_2022_12_to_3.day;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author haomin
 * @date 2022/12/31 23:04
 **/
public class Day12_31_Review {
    class Solution {
        public ListNode getKthFromEnd(ListNode head, int k) {
            ListNode n1 = head;
            ListNode n2 = head;
            while(k-- > 0) n1 = n1.next;
            while(n1 != null) {
                n1 = n1.next;
                n2 = n2.next;
            }
            return n2;
        }
    }

    class Solution2 {
        public ListNode getKthFromEnd(ListNode head, int k) {
            Deque<ListNode> s = new LinkedList<>();
            while(head != null) {
                s.push(head);
                head = head.next;
            }
            while(--k > 0) {
                s.poll();
            }
            return s.peek();
        }
    }
}